<!DOCTYPE html>
<html lang="en" xmlns:th="http:www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>货位管理</title>
    <link rel="stylesheet" href="/js/elementui/index.css">
    <style>
        .input1{
            margin-left: 5px;
            margin-right: 10px;
        }
    </style>
</head>
<body>
<div id="app">
    货架编号：<el-input v-model="shelvesId" placeholder="请输入货架编号" class="input1" style="width: 10%"></el-input>
    货位编号：<el-input v-model="cargoNo" placeholder="请输入货位编号" class="input1" style="width: 10%"></el-input>
    货位状态：<template>
                    <el-select v-model="status" placeholder="请选择货位状态">
                        <el-option label="有货" value="有货"></el-option>
                        <el-option label="无货" value="无货"></el-option>
                    </el-select>
              </template>
    <el-button type="success" plain @click="selectOne">搜索</el-button>
    <br>
    <el-button type="primary" @click="dialogFormVisible = true">新建货位</el-button>
    <el-table
            :data="cargos"
            style="width: 100%">
        <el-table-column
                label="编号"
        >
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.id }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="货位编号"
                >
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.cargoNo }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="所属货架编号"
                >
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.shelvesId }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="所属仓库"
                width="180">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.wareNo }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="负责人"
                width="180">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.person }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="联系电话"
                width="180">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.telephone }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="放置货物"
                width="180">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.product }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="创建时间"
                width="100">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.creatTime }}</span>
            </template>
        </el-table-column>
        <el-table-column
                label="状态"
                width="180">
            <template slot-scope="cargos">
                <span style="margin-left: 10px">{{ cargos.row.status }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作"  width="280">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        type="success"
                        icon="el-icon-edit"
                        @click="handleUpdateStatus(scope.$index, scope.row)"></el-button>
                <el-button
                        size="mini"
                        type="info"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="curr"
            :page-sizes="[5,10,15,20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <el-dialog title="货位信息" :visible.sync="dialogFormVisible" @close="closeMyDialog">
        <el-form :model="cargo">
            <el-form-item label="货位编号" :label-width="formLabelWidth">
                <el-input v-model="cargo.cargoNo" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所属货架编号" :label-width="formLabelWidth">
                <el-input v-model="cargo.shelvesId" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所属仓库" :label-width="formLabelWidth">
                <el-input v-model="cargo.wareNo" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="负责人" :label-width="formLabelWidth">
                <el-input v-model="cargo.person" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" :label-width="formLabelWidth">
                <el-input v-model="cargo.telephone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="放置货物" :label-width="formLabelWidth">
                <el-input v-model="cargo.product" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input v-model="cargo.creatTime" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth">
                <el-input v-model="cargo.status" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveCargo">确 认</el-button>
        </div>
    </el-dialog>
</div>
<script src="/js/vue/vue.js"></script>
<script src="/js/axios/axios.min.js"></script>
<script src="/js/qs/qs.min.js"></script>
<script src="/js/elementui/index.js"></script>
<script>
    new Vue({
        el:"#app",
        data (){
            return {
                cargos:null,
                dialogFormVisible:false,//控制对话框的显示
                curr:1,
                total:0,
                cargoNo:'',
                shelvesId:'',
                status:'',
                pageSize:5,
                //对话框的数据
                cargo:{
                    id:'',
                    cargoNo:'',
                    shelvesId:'',
                    wareNo:'',
                    person:'',
                    telephone:'',
                    product:'',
                    creatTime:'',
                    status:''
                },
                formLabelWidth:'120px'//表单标签的宽度
            }
        },
        methods:{
            selectOne(){
                axios.get("/selectCargos/"+this.cargoNo+"/"+this.shelvesId+"/"+this.status).then(value => {
                    if (value.data.code == 1){
                        this.cargos = value.data.data;
                    }
                })
            },
            handleSizeChange(pageSize){
                console.log(`每页 ${pageSize} 条`);
                this.loadPage(this.curr,pageSize);
            },
            handleCurrentChange(curr){
                console.log(`当前页: ${curr}`);
                this.loadPage(curr,this.pageSize);
            },
            loadPage(curr,pageSize){
                this.curr=curr;
                this.pageSize = pageSize;
                axios.get("/cargos?current="+curr+"&pageSize="+pageSize).then(value => {
                    if (value.data.code == 1){
                        this.cargos = value.data.data.records;
                        this.curr = value.data.data.current;
                        this.pageSize = value.data.data.size;
                        this.total = value.data.data.total;
                    }
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
                this.cargo = row;
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                console.log(index, row);
                if (confirm("是否确认删除")){
                    axios.delete("/cargos/"+row.id).then(value => {
                        if(value.data.code == 1){
                            location.reload()
                        }
                    });
                }
            },
            //关闭对话框
            closeMyDialog(){      //对话框数据
                this.cargo={
                    cargoNo:'',
                    shelvesId:'',
                    wareNo:'',
                    person:'',
                    telephone:'',
                    product:'',
                    creatTime:'',
                    status:'',
                }
            },
            //改变货位状态
            handleUpdateStatus(index,row){
                console.log(row.status)
                if (row.status=="有货"){
                    row.status='无货';
                    axios.put("/cargos/"+row.id+"/"+row.status).then(value => {
                        if (value.data.code==1){
                            this.$message("状态改变成功")
                        }
                    })
                }else{
                    row.status='有货';
                    axios.put("/cargos/"+row.id+"/"+row.status).then(value => {
                        if (value.data.code==1){
                            this.$message("状态改变成功")
                        }
                    })
                }
            },
            //保存货位的方法
            saveCargo() {
                //判断有id进行更新，否则添加
                if (this.cargo.id == '') {
                    axios.post("/cargos",
                        {
                            wareId:this.cargo.wareId,
                            cargoNo: this.cargo.cargoNo,
                            shelvesId: this.cargo.shelvesId,
                            wareNo: this.cargo.wareNo,
                            person: this.cargo.person,
                            telephone: this.cargo.telephone,
                            product: this.cargo.product,
                            creatTime: this.cargo.creatTime,
                            status: this.cargo.status
                        }
                    ).then(value => {
                        if (value.data.code == 1) {
                            location.reload()
                        }
                    });
                } else {
                    axios.put("/cargos",
                        {
                            id: this.cargo.id,
                            wareId:this.cargo.wareId,
                            cargoNo: this.cargo.cargoNo,
                            shelvesId: this.cargo.shelvesId,
                            wareNo: this.cargo.wareNo,
                            person: this.cargo.person,
                            telephone: this.cargo.telephone,
                            product: this.cargo.product,
                            creatTime: this.cargo.creatTime,
                            status: this.cargo.status
                        }
                    ).then(value => {
                        if (value.data.code == 1) {
                            location.reload()
                        }
                    });
                }
                this.dialogFormVisible = false;
            }
        },
        mounted(){
            //页面加载成功回调
            this.loadPage(1,5);
            /*axios.get("/cargos").then(value => {
                if (value.data.code == 1){
                    this.cargos = value.data.data;
                }
            })*/
        }
    })
</script>
</body>
</html>